package com.lagou.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lagou.dao.UserMapper;
import com.lagou.domain.*;
import com.lagou.service.UserService;
import com.lagou.utils.Md5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 用户分页&多条件组合查询
     */
    @Override
    public PageInfo findAllUserByPage(UserVo userVo) {

        PageHelper.startPage(userVo.getCurrentPage(),userVo.getPageSize());

        List<User> allUserByPage = userMapper.findAllUserByPage(userVo);

        PageInfo<User> pageInfo = new PageInfo<>(allUserByPage);

        return pageInfo;
    }

    @Override
    public void updateUserStatus(Integer id, String status) {

        User user = new User();
        user.setId(id);
        user.setStatus(status);
        user.setUpdate_time(new Date());

        userMapper.updateUserStatus(user);
    }

    /**
     * 用户登录（根据用户名查询具体的用户信息）
     */
    @Override
    public User login(User user) throws Exception {

        //1.调用mapper方法 login:包含密文密码
        User login = userMapper.login(user);

        //Md5.verify()中三个参数分别为明文密码 加密key值 密文密码
        if (login != null && Md5.verify(user.getPassword(),"lagou",login.getPassword())){

            //此时密码正确
            return login;
        }else {
            return null;
        }
    }

    /**
     * 分配角色(回显根据用户id查询关联的角色信息)
     * @param id
     * @return
     */
    @Override
    public List<Role> findUserRelationRoleById(Integer id) {

        List<Role> list = userMapper.findUserRelationRoleById(id);
        return list;
    }

    /**
     * 分配角色
     * @param userVo
     */
    @Override
    public void userContextRole(UserVo userVo) {

        //1.根据用户id清空中间表关联关系
        userMapper.deleteUserContextRole(userVo.getUserId());

        //2.在重新建立关联关系
        List<Integer> roleIdList = userVo.getRoleIdList();
        for (Integer roleId : roleIdList) {

            //封装数据
            User_Role_relation user_role_relation = new User_Role_relation();
            user_role_relation.setUserId(userVo.getUserId());
            user_role_relation.setRoleId(roleId);

            Date date = new Date();
            user_role_relation.setCreatedTime(date);
            user_role_relation.setUpdatedTime(date);

            user_role_relation.setCreatedBy("system");
            user_role_relation.setUpdatedby("system");

            userMapper.userContextRole(user_role_relation);

        }
    }

    /**
     * 获取用户权限信息
     * @param userid
     * @return
     */
    @Override
    public ResponseResult getUserPermissions(Integer userid) {

        //1.获取当前用户拥有的角色
        List<Role> roleList = userMapper.findUserRelationRoleById(userid);

        //2.获取角色id保存到list集合中
        List<Integer> roleIds = new ArrayList<>();

        for (Role role : roleList) {
            //把当前正在遍历的role里面的id值进行存储
            roleIds.add(role.getId());
        }

        //3.根据角色id查询父菜单
        List<Menu> parentMenu = userMapper.findParentMenuByRoleId(roleIds);

        //4.查询封装父菜单关联的子菜单
        for (Menu menu : parentMenu) {

            List<Menu> subMenu = userMapper.findSubMenuByPid(menu.getId());

            menu.setSubMenuList(subMenu);
        }

        //5.获取该用户所拥有的资源信息
        List<Resource> resourceList = userMapper.findResourceByRoleId(roleIds);

        //6.封装数据并进行返回
        HashMap<String , Object> map = new HashMap<>();
        map.put("menuList",parentMenu);
        map.put("resourceList",resourceList);

        return new ResponseResult(true,200,"获取用户权限信息成功",map);
    }
}
